<template>
  <a-modal
    :open="open"
    width="640px"
    :title="rowData?.id ? '修改药房' : '新增药房'"
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    centered
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" class="form-style-flex" :label-col="{ style: { width: '100px' } }">
      <a-form-item label="药房名称" class="w-full" v-bind="validateInfos.storeName">
        <a-input v-model:value="formState.storeName" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="药房类别" class="w-3/5">
        <a-checkbox-group v-model:value="formState.storetypeList" :options="typeOptions" />
        <!-- <a-radio-group v-model:value="formState.storetype">
          <a-radio :value="1">西成药</a-radio>
          <a-radio :value="2">中成药</a-radio>
        </a-radio-group> -->
      </a-form-item>
      <a-form-item label="是否本地药房" class="w-2/5">
        <a-radio-group v-model:value="formState.isLocalStore">
          <a-radio :value="0">是</a-radio>
          <a-radio :value="1">否</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="发药仓库" class="w-1/2" v-bind="validateInfos.warehouse">
        <SelectWarehouse v-model:value="formState.warehouse" :disabled="formState.isLocalStore" />
      </a-form-item>
      <a-form-item label="顺序号" class="w-1/2" v-bind="validateInfos.sortNo">
        <a-input v-model:value="formState.sortNo" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="联系人" class="w-1/2" v-bind="validateInfos.contactName">
        <a-input v-model:value="formState.contactName" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="联系电话" class="w-1/2" v-bind="validateInfos.contactPhone">
        <a-input v-model:value="formState.contactPhone" placeholder="请输入" />
      </a-form-item>

      <a-form-item label="地址" class="w-full">
        <a-input v-model:value="formState.contactAddress" placeholder="请输入" />
      </a-form-item>

      <a-form-item label="备注" class="w-full">
        <a-textarea v-model:value="formState.remarks" :rows="1" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const formState = ref({
  storeName: '',
  sortNo: '',
  isLocalStore: 0,
  contactName: '',
  contactPhone: '',
  remarks: '',
  contactAddress: '',
  storetypeList: [],
  id: ''
})
const typeOptions = [
  {
    label: '西成药',
    value: '1'
  },
  {
    label: '中草药',
    value: '2'
  },
  {
    label: '物资',
    value: '3'
  }
]
const rulesState = ref({
  storeName: [{ required: true, message: '请输入药房名称' }],
  sortNo: [{ required: true, message: '请输入顺序号' }],
  contactPhone: [{ required: true, message: '请输入联系人电话' }],
  contactName: [{ required: true, message: '请输入联系人' }]
})
const { isCompleted, resetFields, validate, validateInfos } = useForm(formState, rulesState)

const onOk = async () => {
  await validate()
  const res = await post('/operation/opex/doSaveDrugStoreInfo', formState.value)
  res.code !== 0 ? message.error(res.msg || '操作失败') : message.success('保存成功')
  emit('success')
  onCancel()
}
watch(
  () => props.open,
  (val) => {
    if (val && props.rowData?.id) {
      formState.value = { ...props.rowData, storetype: parseInt(props.rowData.storetype) }
    }
  },
  {
    immediate: true
  }
)

const onCancel = () => {
  resetFields()
  emit('update:open', false)
}
</script>
